<?php
class Db{
    private $db_link;
    private $db_host;
    private $db_use;
    private $db_passwd;

    function __construct()
    {
	$this->db_link = FALSE;
	$this->db_host = "127.0.0.1";
	$this->db_name = 'moz';
	$this->db_use = "root";
	$this->db_passwd = "7511xxw";
    }
    function __destruct()
    {
	//if($this->db_link)
	 //   mysql_close($this->db_link);
    }
    // 连接并选择数据库
    public function db_connect()
    {
	$this->db_link = mysql_connect($this->db_host,$this->db_use,$this->db_passwd);
	if($this->db_link == FALSE)
	    die("打开数据库错误".mysql_error());
	mysql_select_db($this->db_name,$this->db_link) or die('不能使用XueMer数据库'.mysql_error());
    }
    public function db_insert($table,$inserts)
    {
	$values = array_map('mysql_real_escape_string',array_values($inserts));
	$keys = array_keys($inserts);

	return mysql_query('INSERT INTO `'.$table.'`(`'.implode('`,`',$keys).'`) VALUES (\''.implode('\',\'',$values).'\')',$this->db_link);
    }
    // $table 表名
    // $keys 要查询的字段名
    // $where Where条件
    public function db_select($table,$keys,$where)
    {
	return mysql_query('SELECT '.implode('`,`',$keys).' FROM `'.$table.'` WHERE '.$where,$this->db_link);
    }
    public function db_delete($table,$where)
    {
	return mysql_query('DELETE FROM `'.$table.'` WHERE '.$where,$this->db_link);
    }
    public function db_update($table,$key,$value,$where)
    {
	return mysql_query('UPDATE '.$table.' SET '.$key.'='.$value.' WHERE '.$where,$this->db_link);
    }
}
?>
